#!/bin/bash

# dbConnect IBM DB2 database example(s) and testing tables
# Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org
#
# This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
#
#   Note: Make sure the dbconnect user exists as a unix user before creating the DB
#       useradd dbconn
#	     passwd dbconn (set to letmein)
#
#   Note: To create execute `sh ./database.sh` as the db2inst1 user on the database server
#         Once executed, can access the database using `db2 connect to dbConn` on the database server
#         You are required to create a catalog node for a client machine, run the catalognode.sh script
#

database=dbConn
user=dbconn
db2home=/home/db2inst1

echo "Creating database $database"

db2 "create database $database"

echo "Connecting..."

db2 "connect to $database"

echo "Setting user permissions..."

db2 "revoke bindadd, createtab, implicit_schema on database from public"
db2 "grant connect, createtab, bindadd, implicit_schema on database to user $user"

echo "Creating bufferpool..."

db2 "create bufferpool buffpool8k size 1000 pagesize 8k"

echo "Activating bufferpool..."

db2 "disconnect $database"
db2 "connect to $database"

echo "Creating tablespace..."

db2 "create tablespace userspace8k pagesize 8k managed by system using ('$db2home/ts-$database-8k') bufferpool buffpool8k"

echo "Setting tablespace permissions..."

db2 "grant use of tablespace userspace8k to user $user"

echo "Updating config... maxappls"

db2 "update database configuration for $database using maxappls 100"

echo "Updating config... log recovery details"
mkdir -p $db2home/logs-$database

db2 "update database configuration for $database using logretain recovery"
db2 "update database configuration for $database using newlogpath $db2home/logs-$database"
db2 "update database configuration for $database using logfilsiz 500"
db2 "update database configuration for $database using logprimary 16"
db2 "update database configuration for $database using logsecond 4"
db2 "update database configuration for $database using logbufsz 16"


echo "Disconnecting..."

db2 "disconnect $database"

echo "Performing Initial Backup..."

db2 backup db $database to /tmp/

echo "Activating..."

db2 "activate database $database"

echo "Done"
